<?php

/*
 * File  : berita.php
 * Table : beritas, kategoris
 */
//session_start();
//cek session

if ((!$_SESSION["USRID"] AND !$_SESSION["USRPASS"])) {
    header("location: ../index.php");
    exit();
}
//end
?>

<?php

$aksi = "cms/app/m_beritas/aksi_berita.php";
switch ($_GET["opr"]) {

// tampil berita
    default:
        echo "<h2>Berita</h2>";
        echo " <input type=button value='Tambah Berita' onclick=\"window.location.href='?module=berita&opr=tambah';\">";
        echo " <table>";
        echo "  <tr><th>No</th><th>Kategori</th><th>Judul</th><th>Tanggal</th><th>Aksi</th></tr>";

        $p = new Paging;
        $batas = 10;
        $posisi = $p->cariPosisi($batas);

        $tampil = mysql_query("SELECT * FROM beritas ORDER BY berita_id DESC LIMIT $posisi, $batas");

        $no = $posisi + 1;
        while ($row = mysql_fetch_array($tampil)) {
            $tanggal = get_format_waktu_full($row["berita_tgl"]);
            echo "<tr><td>$no</td>";
            echo " <td>" . get_kategori_berita_nama_from_id($row["berita_kategori_id"]) . "</td>";
            echo " <td>" . $row["berita_judul"] . "</td>";
            echo " <td>$tanggal</td>";
            echo " <td><a href='?module=berita&opr=edit&id=" . $row["berita_id"] . "'>Edit</a> | 
		                <a href='" . URLHOST . "$aksi?module=berita&opr=hapus&id=" . $row["berita_id"] . "'  onClick=\"return confirm('Hapus berita " . $row["berita_judul"] . "?')\" >Hapus</a>";
            echo " </td></tr>";
            $no++;
        }
        echo "</table>";

        $jmldata = mysql_num_rows(mysql_query("SELECT * FROM beritas"));
        $jmlhalaman = $p->jumlahHalaman($jmldata, $batas);
        $linkHalaman = $p->navHalaman($_GET["halaman"], $jmlhalaman);

        echo "<div id=paging>Hal: $linkHalaman</div><br>";
        break;

// tambah berita
    case "tambah":
        load_tinymce();
        echo "<h2>Tambah Berita</h2>";
        echo " <form method='POST' action='" . URLHOST . "$aksi?module=berita&opr=input' enctype='multipart/form-data'>";
        echo " <table>";
        echo "  <tr><td>Nama berita</td><td> : <input type='text' name='in_berita_judul' size='100'></td></tr>";
        echo "  <tr><td>Kategori</td>  <td> :";
        echo "   <select name='in_berita_kategori_id'>";
        echo "    <option value='0' selected>- Pilih Kategori -</option>";
        $tampil = mysql_query("SELECT * FROM kategoris ORDER BY kategori_nama");
        while ($row = mysql_fetch_array($tampil)) {
            echo "<option value='" . $row["kategori_id"] . "'>" . $row["kategori_nama"] . "</option>";
        }
        echo "   </select></td></tr>";
        echo "  <tr><td>Isi</td><td> <textarea name = 'in_berita_isi' style = 'width: 600px; height: 350px;'></textarea></td></tr>";
        echo "  <tr><td colspan='2'><input type ='submit' value ='Simpan'>";
        echo "  <input type ='button' value ='Batal' onclick ='self.history.back()'></td></tr>";
        echo " </table>";
        echo " </form>";
        break;

    case "edit":
        $edit = mysql_query("SELECT * FROM beritas WHERE berita_id = '" . $_GET["id"] . "'");
        $row = mysql_fetch_array($edit);

        load_tinymce();
        echo "<h2>Edit Berita</h2>";
        echo " <form method='POST' action ='" . URLHOST . "$aksi?module=berita&opr=update' enctype='multipart/form-data'>";
        echo " <input type ='hidden' name ='id' value ='" . $row["berita_id"] . "'>";
        echo " <table>";
        echo "  <tr><td>Judul</td> <td> : <input type='text' name = 'in_berita_judul' size ='100' value = '" . $row["berita_judul"] . "'></td></tr>";
        echo "  <tr><td>Kategori</td> <td> : <select name = 'in_berita_kategori_id'>";

        $tampil = mysql_query("SELECT * FROM kategoris ORDER BY kategori_nama");
        if ($row["berita_kategori_id"] == 0) {
            echo "<option value = 0 selected>- Pilih Kategori -</option>";
        }

        while ($w = mysql_fetch_array($tampil)) {
            if ($row["berita_kategori_id"] == $w["kategori_id"]) {
                echo "<option value ='" . $w["kategori_id"] . "' selected>" . $w["kategori_nama"] . "</option>";
            } else {
                echo "<option value ='" . $w["kategori_id"] . "'>" . $w["kategori_nama"] . "</option>";
            }
        }
        echo "  </select></td></tr>";
        echo "  <tr><td>Isi</td> <td> <textarea name = 'in_berita_isi' style = 'width: 600px; height: 350px;'>" . $row["berita_isi"] . "</textarea></td></tr>";
        echo "  <tr><td colspan = 2><input type ='submit' value ='Update'><input type ='button' value ='Batal' onclick = self.history.back()></td></tr>";
        echo "  </table></form>";
        break;
}
?>
